package com.examsystem.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author 86134
 */
public class DataUtil {

    public String makeTime(){
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat simpleDateFormat3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDateFormat3.format(calendar.getTime());
    }

    public Date returnTime(String datetime){
        SimpleDateFormat simpleDateFormat4= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar2 = Calendar.getInstance();
        Date date4 = null;
        try {
            date4 =simpleDateFormat4.parse(datetime);
            calendar2.setTime(date4);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date4;
    }


    public String changeTime(String time,int examTime){

        SimpleDateFormat dateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date endDate = null;
        try {
            endDate = dateTime.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //减一分钟
        assert endDate != null;
        endDate.setTime(endDate.getTime() + 1000L * 60 * examTime );
        return dateTime.format(endDate);
    }

    public boolean checkTime(String start,String deadline) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        try {
            c1.setTime(sdf.parse(deadline));
            c2.setTime(sdf.parse(start));
        } catch (Exception e) {
            e.printStackTrace();
        }
        int result = c2.compareTo(c1);
        return result <= 0;
    }

    public static void main(String[] args) {
        DataUtil dataUtil = new DataUtil();
        System.out.println(dataUtil.makeTime());
        System.out.println(dataUtil.returnTime("2022-10-04 13:13:13"));

    }
}
